package com.cn.wanxi.service;

import com.cn.wanxi.IO.FileModelIO;
import com.cn.wanxi.IOModel.ProductIOModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.IOUtil;

import java.util.List;

public class ProductService {
    public List<ProductModel> getProductList() {
        FileModelIO.writeData(IOUtil.PRODUCTPATH.getPath(), new ProductIOModel().toString());
        String result = FileModelIO.readData(IOUtil.PRODUCTPATH.getPath());
        String[] pdtData = FileModelIO.inputDataList(result, IOUtil.SPILT.getPath());
        String photo = pdtData[0].substring(pdtData[0].indexOf("[")+1, pdtData[0].length());
        String name = pdtData[1].substring(pdtData[1].indexOf("[")+1, pdtData[1].length());
        String user_type = pdtData[2].substring(pdtData[2].indexOf("[")+1, pdtData[2].length());
        String[] names = name.split(",");
        String[] photos = photo.split(",");
        String[] user_types = user_type.split(",");
        ProductModel productModel = new ProductModel();
        for (int i = 0; i < names.length; i++) {
            ProductModel product = new ProductModel();
            product.setName(names[i].trim());
            product.setPhoto(photos[i].trim());
            product.setUser_type(user_types[i].trim());
            product.setPrice("0.0");
            productModel.list.add(product);
            System.out.println(names[i].trim() + ":" + photos[i].trim());
        }
        return productModel.list;
    }

    public static void main(String[] args) {
        new ProductService().getProductList();
    }
}
